import { cookie } from 'vux'
export default {
    state : {
        vComment:{},//评价的数量等
        availableQty:0,//库存
        on:true,//加入购物车的背景
        histroyshoplist: [],
        cookieshoplist:[],
        shoplistNow:'',//当前搜索的文子
        shoptype:'',//店的类型
        yztype:'',//是否是云砖兑换活动,
        yr: false,//预热
        telcall:'',//联系苏鲜生
        xsqgnum: 0,//限时抢购数量
        oneHourUp:[],//分类页的数据给列表页的tab使用的,
    },
    mutations : {

        //评价概况
        vComment (state, payload) {
            state.vComment = payload
        },
        availableQty (state, payload) {
            state.availableQty = payload.availableQty
        },

        on (state, payload) {
            state.on = payload;
        },

        setshoplist(state, second){
          if(second.text1) {
            var data1 = second.text1
            // var data2 = data1.replace(/\,/g, '@');
            state.histroyshoplist.unshift(data1);
            
            state.cookieshoplist.unshift(data1);//[{ a:b . c:d }]

            state.histroyshoplist.length > 9? state.histroyshoplist.splice(9,1) : 0
            state.cookieshoplist.length > 9? state.cookieshoplist.splice(9,1) : 0

            document.cookie = 'foo='+state.cookieshoplist
          } 
        },
        clearshoplist(state) {
          state.histroyshoplist=[];
          state.cookieshoplist=[];
          document.cookie = 'foo=';
        },

        setshoplistNow(state, payload) {
                state.shoplistNow = payload.text1;
                document.cookie = 'nowtext='+ payload.text1;
        },
        shoptype(state, payload) {
            if(payload.typetext) {
                state.shoptype = payload.typetext;
             }
        },
        yztype (state, payload){
                state.yztype = payload.yztype;
        },
        xsqgnum (state, payload){
                state.xsqgnum = payload.xsqgnum;
        },
        yr (state, payload){
                state.yr = payload
        },
        telcall (state, payload){
            if(payload) {
                state.telcall = payload
            }
        },
        oneHourUp (state, payload){
            if(payload) {
                state.oneHourUp = payload
            }
        }

    },
    actions: {

        vComment ({ commit, state }, options) {
            commit('vComment', options)
        },
        availableQty ({ commit, state }, options) {
            commit('availableQty', options)
        },


        on ({ commit, state }, payload) {
            commit('on', payload)
        },


        setshoplist({commit, state}, options){
       
          if(!options)return
         
          let list = state.histroyshoplist;
          let text1 = options.text1.replace(/(^\s+)|(\s+$)/g, "");
          if(list.length > 0){
              for( let i = 0; i < list.length; i++ ){
               
                  if(text1 == list[i]){

                     
                        state.histroyshoplist.splice(i, 1)

                        state.cookieshoplist.splice(i, 1)
                    
                    // text1='';//找到相同的立马撤
                    break;
                  } 
              }
          }
 
            commit('setshoplist', {text1: text1})
 
        
          
        },
        clearshoplist({commit, state}, options){
          commit('clearshoplist')
        },

        setshoplistNow({commit, state}, options){
            commit('setshoplistNow', options)
        },

        shoptype({commit, state}, options){
            commit('shoptype', options)
        },

        yztype ({commit, state}, options){
            if(options.yztype) {
                commit('yztype', options)
            }
        },

        xsqgnum ({commit, state}, options){
            if(options) {

                commit('xsqgnum', options)
            }
        },
        yr ({commit, state}, options){
             commit('yr', options)
        },
        telcall ({commit, state}, options){
             commit('telcall', options)
        },

        oneHourUp ({commit, state}, options){
             commit('oneHourUp', options)
        },
        
    },  
    getters: {

        // 购物车列表
        getvComment: state => state.vComment,
        //库存
        getavailableQty: state => state.availableQty,

        geton: state => state.on,


        getshoplist: function(state){
          let foo = cookie.get('foo');
          if(foo && state.histroyshoplist.length==0 && state.cookieshoplist.length == 0){
            var data1 = foo.split(",");
            console.log(data1)
                
            state.cookieshoplist = data1 

            for(var i = 0; i <  data1.length; i++  ){
                state.histroyshoplist.push(  data1[i] ); 
            }

          } 
          console.log(state.histroyshoplist)
          return state.histroyshoplist
        },

        getshoplistNow: function(state){
          let nowtext = cookie.get('nowtext');//{"text1":"苹果","title":"一小时达"}
          if(nowtext && state.shoplistNow==''){
              state.shoplistNow = nowtext;
          } 
          return state.shoplistNow
        },

        gettype: state => state.shoptype,

        getyztype: state => state.yztype,

        centercarnum: state => state.centercarnum,

        yr: state => state.yr,

        telcall: state => state.telcall,

        oneHourUp: state => state.oneHourUp
    


    }
}
 